define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vehicle/index' + location.search,
                    add_url: 'vehicle/add?model_Id=' + Config.model_Id,
                    edit_url: 'vehicle/edit',
                    import_url: 'example/import',
                    // del_url: 'vehicle/del',
                    multi_url: 'vehicle/multi',
                    import_url: 'vehicle/import',
                    dragsort_url: '',
                    table: 'vehicle',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                pagination: false,
                commonSearch: false,
                escape: false,
                columns: [
                    [
                        // {field: 'id',
                        //     title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle"><i class="fa fa-chevron-up"></i></a>',
                        //     operate: false,
                        //     formatter: Controller.api.formatter.subnode
                        // },
                        {field: 'id', title:__('Id')},
                        {field: 'mergename', title: __('mergename'), align: 'left', formatter:function (value, row, index) {
                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                            }
                        },
                        {field: 'level', title: __('Level'), searchList: {"1":__('厂牌'),"2":__('车型'), "3":__('年份'), "4":__('排量'), "5":__('款型')}, formatter: Table.api.formatter.status},
                        {field: 'pid', title: __('Pid'),searchList: {"0":__('顶级')},formatter: Table.api.formatter.status},
                        {field: 'del', title: __('Del'),formatter:Table.api.formatter.toggle,yes: '0', no: '1'},
                        {field: 'flag', title: __('Flag'), searchList: {"hot":__('Flag hot'), 'nohot':__('Flag nohot')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                // {
                                //     name: 'del',
                                //     text: __('删除'),
                                //     title: __('删除'),
                                //     confirm: '您确定删除吗',
                                //     classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                //     icon: 'fa fa-close',
                                //     url: $.fn.bootstrapTable.defaults.extend.del_url,
                                //     refresh: true,
                                //     visible: true
                                // },
                                {
                                    name: 'add',
                                    title: function(row){
                                        // console.log(row.mergename)
                                        if (row.level == 1)
                                        {
                                            var data =  "厂牌";
                                        }
                                        if (row.level == 2)
                                        {
                                            var data =  "车型";
                                        }
                                        if (row.level == 3)
                                        {
                                            var data =  "年份";
                                        }
                                        if (row.level == 4)
                                        {
                                            var data =  "排量";
                                        }
                                        if (row.level == 5)
                                        {
                                            var data =  "款型";
                                        }
                                        return row.mergename + '-' + data;
                                    },
                                    text: __('查看下一级'),
                                    classname: 'btn btn-info btn-xs btn-dialog',
                                    extend:'data-area=\'["100%", "100%"]\'',
                                    icon: 'fa fa-plus',
                                    url: 'vehicle/index?id={ids}',
                                    visible: function (data){
                                        if (data.level > 4)
                                        {
                                            return false;
                                        }else{
                                            return true;
                                        }
                                    }
                                }

                            ]

                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);

        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('切换菜单') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" data-pids="' + row.pids + '" class="btn btn-xs '
                        + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
                },
            },

            bindevent: function () {
                $('.btn-select').change(function(e){
                    var pid = document.getElementById('c-pid').value;
                    var str = pid.split('.');
                    var t = document.getElementById('c-logos');
                    if (str[1] == 0 || str[1] == 4)
                    {
                        t.style.display = 'block';
                    }else{
                        t.style.display = 'none';
                    }
                });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});